
package org.abbett.eskine.controller;

import org.abbett.eskine.model.CustomIdentity;
import org.abbett.eskine.entity.User;
import org.abbett.eskine.dao.UserDao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;


import javax.faces.component.UIComponent;
import javax.faces.component.UIData;
import javax.faces.event.ActionEvent;
import java.util.Map;
import java.util.List;

import org.abbett.eskine.service.HomeLoginLoginOutcome;
import org.abbett.eskine.service.HomeSignUpSignUpOutcome;
import org.abbett.eskine.service.HomeService;


/**
 * Glue code to bind Log In or Sign Up Service with the view framework (Seam). This class contains
 * a default implementation of the action methods. Please avoid reimplmenting the action methods in this class.
 * The purpose of this class is to declare properties which can be bound to the UI.
 * 
  * This page provides a login form and a sign-up form.
  *
 * Automatically generated by clickframes
 * 
 * @author Vineet Manohar
 */
public abstract class GeneratedHomeController extends Controller {

    /** 
                An Eskine user. Each user had pages.
             */
    private User user;

    /** 
     * Get User
     *  
     * @return User
     */
    public User getUser() {
       return this.user;
    }

    /**
     * Set User
     *
     * @param user
     */
    public void setUser(User user) {
        this.user = user;
    }

 
    @In("#{userDao}") 
    protected UserDao userDao;


    
    @In("#{homePageService}")
	protected HomeService homeService;

	@SuppressWarnings("unused")
	private static final Log logger = LogFactory.getLog(GeneratedHomeController.class);

	@In
	protected CustomIdentity identity;

    @Create
    public void init() {


        // initialize all file inputs
 

        // initialize all file inputs
          // all inputs initialized here 
    }




	/**
	 * This method should be called before the page is loaded
	 * 
	 * All the page parameters should be consumed in this method
	 */
    public void processPageParameters() {


          // if you need to add more code, override this method in the child class 
    }



	/** E-mail
	 *
 	 * E-mail
 	 */
   
	protected String loginEmail;

  	@org.hibernate.validator.NotNull(message="Please enter your e-mail address.")
  	public String getLoginEmail() {
	   return this.loginEmail;
	}

	public void setLoginEmail(String loginEmail) {
		this.loginEmail = loginEmail;
	}
	/** Password
	 *
 	 * Password
 	 */
   
	protected String loginPassword;

  	@org.hibernate.validator.NotNull(message="Please enter your password.")
  	public String getLoginPassword() {
	   return this.loginPassword;
	}

	public void setLoginPassword(String loginPassword) {
		this.loginPassword = loginPassword;
	}



	/** E-mail
	 *
 	 * E-mail
 	 */
   
	protected String signUpEmail;

  	@org.hibernate.validator.NotNull(message="Please enter your e-mail address.")
   	@org.hibernate.validator.Email(message="Please enter a valid e-mail address.")
   	public String getSignUpEmail() {
	   return this.signUpEmail;
	}

	public void setSignUpEmail(String signUpEmail) {
		this.signUpEmail = signUpEmail;
	}
	/** Password
	 *
 	 * Password
 	 */
   
	protected String signUpPassword;

  	@org.hibernate.validator.NotNull(message="Please enter your password.")
     	@org.hibernate.validator.Length( min=4,  message="Password must be at least four characters.")
 	public String getSignUpPassword() {
	   return this.signUpPassword;
	}

	public void setSignUpPassword(String signUpPassword) {
		this.signUpPassword = signUpPassword;
	}
	/** Repeat Password
	 *
 	 * Repeat Password
 	 */
   
	protected String signUpRepeatPassword;

  	@org.hibernate.validator.NotNull(message="Please repeat your password.")
  	public String getSignUpRepeatPassword() {
	   return this.signUpRepeatPassword;
	}

	public void setSignUpRepeatPassword(String signUpRepeatPassword) {
		this.signUpRepeatPassword = signUpRepeatPassword;
	}



  
    /**
     *  Converts any form inputs bound to entities to entities, stores them in the map ready to 
     *  be passed to the service methods
     */
    public Map<String, Object> loginForm() {
        Map<String, Object> map = describe();
     // create a new User from input
    User newUser = userDao.newInstance();
        newUser.setEmail(toText(loginEmail));
        newUser.setPassword(toText(loginPassword));
        map.put("user", newUser);
        return map;
    }

    public HomeLoginLoginOutcome loginLogin() {
      // call seam
      String seamOutcome = identity.login();

      if (seamOutcome != null && seamOutcome.equals("loggedIn")) {
          return convertSuccessfulSeamOutcomeToApplicationOutcome();
      }

      // convert seam outcome into clickframes outcome
      return HomeLoginLoginOutcome.loginFailed;
   }
 
    /**
     *  Converts any form inputs bound to entities to entities, stores them in the map ready to 
     *  be passed to the service methods
     */
    public Map<String, Object> signUpForm() {
        Map<String, Object> map = describe();
     // create a new User from input
    User newUser = userDao.newInstance();
        newUser.setEmail(toText(signUpEmail));
        newUser.setPassword(toText(signUpPassword));
        map.put("user", newUser);
        return map;
    }

 
   /**
    * Sign Up
    *
    *     * Automatically generated by clickframes
    */
   public abstract HomeSignUpSignUpOutcome signUpSignUp();

  public boolean authenticate() {
    HomeService.LoginLoginResponse loginResponse 
            = homeService.loginLogin(loginForm());
    HomeLoginLoginOutcome loginOutcome = loginResponse.getOutcome();

    // store user for the session
    identity.setUser(loginResponse.getUser());
    // identity.setRememberMe(false);
   
    // return positive outcome
    return loginOutcome == HomeLoginLoginOutcome.loginSuccessful;
  }

    protected HomeService.LoginLoginResponse loginLoginResponse;

    public HomeService.LoginLoginResponse getLoginLoginResponse() {
        return loginLoginResponse;
    }

    protected HomeService.SignUpSignUpResponse signUpSignUpResponse;

    public HomeService.SignUpSignUpResponse getSignUpSignUpResponse() {
        return signUpSignUpResponse;
    }


    protected HomeLoginLoginOutcome convertSuccessfulSeamOutcomeToApplicationOutcome() {
        return HomeLoginLoginOutcome.loginSuccessful;
    }

    private static final long serialVersionUID = 1L;
}// clickframes::version=4149174992::clickframes